﻿@model ProductAttributeModel

@Html.ValidationSummary(false)
@Html.HiddenFor(model => model.Id)

<script>
    $(document).ready(function() {
        bindBootstrapTabSelectEvent('product-attribute-edit');
    });
</script>

<div class="content">
    <div class="form-horizontal">
        <div id="product-attribute-edit" class="nav-tabs-custom">
            <ul class="nav nav-tabs">
                @Html.RenderBootstrapTabHeader("tab-info", @T("Admin.Catalog.Attributes.ProductAttributes.Info"), true)
                @if (Model.Id > 0)
                {
                    @Html.RenderBootstrapTabHeader("tab-used-by-products", @T("Admin.Catalog.Attributes.ProductAttributes.UsedByProducts"))
                }
                @Html.RenderBootstrapTabHeader("tab-predefined-values", @T("Admin.Catalog.Attributes.ProductAttributes.PredefinedValues"))
            </ul>
            <div class="tab-content">
                @Html.RenderBootstrapTabContent("tab-info", @TabInfo(), true)
                @if (Model.Id > 0)
                {
                    @Html.RenderBootstrapTabContent("tab-used-by-products", @TabUsedByProducts())
                }
                @Html.RenderBootstrapTabContent("tab-predefined-values", @TabPredefinedValues())
            </div>
        </div>
    </div>
</div>

@{
    //custom tabs
    var eventMessage = new AdminTabStripCreated(this.Html, "product-attribute-edit");
    EngineContext.Current.Resolve<IEventPublisher>().Publish(eventMessage);
    foreach (var eventBlock in eventMessage.BlocksToRender)
    {
        @eventBlock
    }
}

@*save selected tab name*@
<input type="hidden" id="selected-tab-name" name="selected-tab-name" value="@(Html.GetSelectedTabName())">

@helper TabInfo()
{
    <div class="panel-group">
        <div class="panel panel-default">
            <div class="panel-body">
                @(Html.LocalizedEditor<ProductAttributeModel, ProductAttributeLocalizedModel>("productattribute-localized",
                @<div>
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.NopLabelFor(model => model.Locales[item].Name)
                        </div>
                        <div class="col-md-9">
                            @Html.NopEditorFor(model => model.Locales[item].Name)
                            @Html.ValidationMessageFor(model => model.Locales[item].Name)
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.NopLabelFor(model => model.Locales[item].Description)
                        </div>
                        <div class="col-md-9">
                            @Html.EditorFor(model => model.Locales[item].Description, "RichEditor")
                            @Html.ValidationMessageFor(model => model.Locales[item].Description)
                        </div>
                    </div>
                    @Html.HiddenFor(model => model.Locales[item].LanguageId)
                </div>
,
                @<div>
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.NopLabelFor(model => model.Name)
                        </div>
                        <div class="col-md-9">
                            @Html.NopEditorFor(model => model.Name, required: true)
                            @Html.ValidationMessageFor(model => model.Name)
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.NopLabelFor(model => model.Description)
                        </div>
                        <div class="col-md-9">
                            @Html.EditorFor(x => x.Description, "RichEditor")
                            @Html.ValidationMessageFor(model => model.Description)
                        </div>
                    </div>
                </div>
))
            </div>
        </div>
    </div>
}

@helper TabUsedByProducts()
{
var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;

    <div class="panel-group">
        <div class="panel panel-default">
            <div class="panel-body">
                <p>
                    @T("Admin.Catalog.Attributes.ProductAttributes.UsedByProducts.Hint")
                </p>
                <div id="used-by-products-grid"></div>

                <script>
                    $(document).ready(function () {
                        $("#used-by-products-grid").kendoGrid({
                            dataSource: {
                                type: "json",
                                transport: {
                                    read: {
                                        url: "@Html.Raw(Url.Action("UsedByProducts", "ProductAttribute", new { productAttributeId = Model.Id }))",
                                        type: "POST",
                                        dataType: "json",
                                        data: addAntiForgeryToken
                                    }
                                },
                                schema: {
                                    data: "Data",
                                    total: "Total",
                                    errors: "Errors"
                                },
                                error: function(e) {
                                    display_kendoui_grid_error(e);
                                    // Cancel the changes
                                    this.cancelChanges();
                                },
                                pageSize: @(defaultGridPageSize),
                                serverPaging: true,
                                serverFiltering: true,
                                serverSorting: true
                            },
                            pageable: {
                                refresh: true,
                                pageSizes: [@(gridPageSizes)],
                                @Html.Partial("_GridPagerMessages")
                            },
                            editable: {
                                confirmation: "@T("Admin.Common.DeleteConfirmation")",
                                mode: "inline"
                            },
                            scrollable: false,
                            columns: [{
                                field: "ProductName",
                                title: "@T("Admin.Catalog.Attributes.ProductAttributes.UsedByProducts.Product")",
                                width: 400
                            }, {
                                field: "Published",
                                title: "@T("Admin.Catalog.Attributes.ProductAttributes.UsedByProducts.Published")",
                                width: 100,
                                headerAttributes: { style: "text-align:center" },
                                attributes: { style: "text-align:center" },
                                template: '# if(Published) {# <i class="fa fa-check true-icon"></i> #} else {# <i class="fa fa-close false-icon"></i> #} #'
                            },
                            {
                                field: "Id",
                                title: "@T("Admin.Common.View")",
                                width: 100,
                                headerAttributes: { style: "text-align:center" },
                                attributes: { style: "text-align:center" },
                                template: '<a class="btn btn-default" href="@Url.Content("~/Admin/Product/Edit/")#=Id#"><i class="fa fa-eye"></i>@T("Admin.Common.View")</a>'
                            }]
                        });
                    });
                </script>
            </div>
        </div>
    </div>
}

@helper TabPredefinedValues()
{
    <div class="panel-group">
        <div class="panel panel-default">
            @if (Model.Id > 0)
            {
                <div class="panel-body">
                    <p>
                        @T("Admin.Catalog.Attributes.ProductAttributes.PredefinedValues.Hint")
                    </p>
                    <div id="productattributevalues-grid"></div>

                    <script>
                        $(document).ready(function() {
                            $("#productattributevalues-grid").kendoGrid({
                                dataSource: {
                                    type: "json",
                                    transport: {
                                        read: {
                                            url: "@Html.Raw(Url.Action("PredefinedProductAttributeValueList", "ProductAttribute", new {productAttributeId = Model.Id}))",
                                            type: "POST",
                                            dataType: "json",
                                            data: addAntiForgeryToken
                                        },
                                        destroy: {
                                            url: "@Html.Raw(Url.Action("PredefinedProductAttributeValueDelete", "ProductAttribute"))",
                                            type: "POST",
                                            dataType: "json",
                                            data: addAntiForgeryToken
                                        }
                                    },
                                    schema: {
                                        data: "Data",
                                        total: "Total",
                                        errors: "Errors",
                                        model: {
                                            id: "Id",
                                            fields: {
                                                Product2Name: { editable: false, type: "string" },
                                                ProductId2: { editable: false, type: "number" }
                                            }
                                        }
                                    },
                                    error: function(e) {
                                        display_kendoui_grid_error(e);
                                        // Cancel the changes
                                        this.cancelChanges();
                                    },
                                    serverPaging: true,
                                    serverFiltering: true,
                                    serverSorting: true
                                },
                                pageable: {
                                    refresh: true,
                                    numeric: false,
                                    previousNext: false,
                                    info: false,
                                    @Html.Partial("_GridPagerMessages")
                                },
                                editable: {
                                    confirmation: "@T("Admin.Common.DeleteConfirmation")",
                                    mode: "inline"
                                },
                                scrollable: false,
                                columns: [
                                    {
                                        field: "Name",
                                        title: "@T("Admin.Catalog.Attributes.ProductAttributes.PredefinedValues.Fields.Name")",
                                        width: 200
                                    }, {
                                        field: "PriceAdjustmentStr",
                                        title: "@T("Admin.Catalog.Attributes.ProductAttributes.PredefinedValues.Fields.PriceAdjustment")",
                                        width: 150
                                    }, {
                                        field: "WeightAdjustmentStr",
                                        title: "@T("Admin.Catalog.Attributes.ProductAttributes.PredefinedValues.Fields.WeightAdjustment")",
                                        width: 150
                                    }, {
                                        field: "IsPreSelected",
                                        title: "@T("Admin.Catalog.Attributes.ProductAttributes.PredefinedValues.Fields.IsPreSelected")",
                                        width: 100,
                                        headerAttributes: { style: "text-align:center" },
                                        attributes: { style: "text-align:center" },
                                        template: '# if(IsPreSelected) {# <i class="fa fa-check true-icon"></i> #} else {# <i class="fa fa-close false-icon"></i> #} #'
                                    }, {
                                        field: "DisplayOrder",
                                        title: "@T("Admin.Catalog.Attributes.ProductAttributes.PredefinedValues.Fields.DisplayOrder")",
                                        width: 100
                                    }, {
                                        field: "Id",
                                        title: "@T("Admin.Common.Edit")",
                                        width: 100,
                                        headerAttributes: { style: "text-align:center" },
                                        attributes: { style: "text-align:center" },
                                        template: "<button onclick=\"javascript:OpenWindow('@Url.Content("~/Admin/ProductAttribute/PredefinedProductAttributeValueEditPopup/")#=Id#?btnId=btnRefresh&formId=productattribute-form', 800, 550, true); return false;\" class='btn btn-default'><i class=\"fa fa-pencil\"></i>@T("Admin.Common.Edit")</button>"
                                    }, {
                                        command: { name: "destroy", text: "@T("Admin.Common.Delete")" },
                                        title: "@T("Admin.Common.Delete")",
                                        width: 200
                                    }
                                ]
                            });
                        });
                    </script>
                </div>
                <div class="panel-footer">
                    <button type="submit" id="btnAddNewValue" onclick="javascript:OpenWindow('@(Url.Action("PredefinedProductAttributeValueCreatePopup", "ProductAttribute", new {productAttributeId = Model.Id, btnId = "btnRefresh", formId = "productattribute-form"}))', 800, 550, true); return false;" class="btn btn-primary">
                        @T("Admin.Catalog.Attributes.ProductAttributes.PredefinedValues.AddNew")
                    </button>
                    <input type="submit" id="btnRefresh" style="display:none" />
                    <script type="text/javascript">
                        $(document).ready(function() {
                            $('#btnRefresh').click(function() {
                                //refresh grid
                                var grid = $("#productattributevalues-grid").data('kendoGrid');
                                grid.dataSource.read();

                                //return false to don't reload a page
                                return false;
                            });
                        });
                    </script>
                </div>
            }
            else
            {
                <div class="panel-body">
                    @T("Admin.Catalog.Attributes.ProductAttributes.PredefinedValues.SaveBeforeEdit")
                </div>
            }
        </div>
    </div>
}
